package cn.wolfcode.luowowo2.articleserver.service.imp;

import cn.wolfcode.luowowo2.article.domain.Destination;
import cn.wolfcode.luowowo2.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo2.article.service.IDestinationService;
import cn.wolfcode.luowowo2.common.query.DestinationQueryObject;
import cn.wolfcode.luowowo2.common.query.QueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Albert on 2019/7/8.
 */
@Service
public class DestinationServiceImpl implements IDestinationService{
    @Autowired
    private DestinationMapper mapper;
    @Override
    public List<Destination> getDestByDeep(Integer deep) {
        return mapper.getDestByDeep(deep);
    }

    @Override
    public List<Destination> getDestByIds(Long[] refIds) {
        return mapper.selectDestByIds(refIds);
    }

    @Override
    public PageInfo<Destination> query(DestinationQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Destination> list = mapper.selectForlist(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void changeHotValue(Boolean hot, Long id) {
        mapper.updateHotValue(hot,id);
    }

    @Override
    public void setInfo(String info, Long id) {
        mapper.setInfo(info,id);
    }

    @Override
    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Destination> getToastList(Long parentId) {
        List<Destination> toasts = new ArrayList<>();
        createToast(parentId,toasts);
        Collections.reverse(toasts);
        toasts.remove(toasts.size()-1);
        return toasts;
    }

    @Override
    public List<Destination> getHotDestByRegionId(Long[] destIds) {
        return mapper.getHotDestByRegionId(destIds);
    }

    @Override
    public Long[] getChildIdsByParentId(Long parentId) {
        return mapper.getChildIdsByParentId(parentId);
    }

    @Override
    public Destination get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean isAbroad(Long id) {
        Boolean flag = toastCheck(id);
        return flag;
    }

    @Override
    public Destination getCountry(Long id) {
        Destination destination = mapper.selectByPrimaryKey(id);
        if (destination==null) {
            return null;
        }
        if (destination.getParent()==null) {
            return destination;
        }
        List<Destination> toastList = this.getToastList(id);
        return toastList.get(0);
    }

    @Override
    public Destination getProvince(Long id) {
        Destination destination = mapper.selectByPrimaryKey(id);
        if (destination==null) {
            return null;
        }
        if (destination.getParent()==null) {
            return null;
        }
        List<Destination> toasts = new ArrayList<>();
        createToast(id,toasts);
        Collections.reverse(toasts);
        return toasts.get(1);
    }

    @Override
    public List<Destination> list() {
        return mapper.selectAll();
    }

    private Boolean toastCheck(Long id) {
        Boolean flag = null;
        Destination destination = mapper.selectByPrimaryKey(id);
        if ("中国".equals(destination.getName())) {
            return false;
        }
        if (destination.getParent()==null) {
            return true;
        }
        flag = toastCheck(destination.getParent().getId());
        return flag;
    }

    /*private void createToast(Long parentId, List<Destination> toasts) {
        Destination parent = mapper.selectByPrimaryKey(parentId);
        if (parent==null) {
            return;
        }
        toasts.add(parent);
        if (parent.getParent()!=null) {
            createToast(parent.getParent().getId(),toasts);
        }
    }*/

    private void createToast(Long id, List<Destination> toasts) {
        Destination destination = mapper.selectByPrimaryKey(id);
        toasts.add(destination);
        if (destination.getParent()==null) {
            return;
        }
        createToast(destination.getParent().getId(),toasts);
    }
}
